#' U4_harmonise_data合并暴露和结局为dat
#'
#' @param exposure_dat  U2_extract_instruments功能提取的工具变量
#' @param outcome_dat  U3_extract_outcomes_data功能提取的结果
#' @param action 默认为2，参考TwoSampleMR的harmonise_data的说明
#'
#' @return harmonise后的data
#' @export
#'
#' @examples
#'
#' \dontrun{
#'
#' library(Oneclick)
#'
#' IVs<-U2_extract_instruments(c("ieu-a-31","ieu-a-300","ieu-a-2"))
#'
#'
#' Outs<-U3_extract_outcomes_data(outcome = c("ieu-a-297","ieu-a-298","ieu-a-299") ,exposure_iv = IVs)
#'
#'
#' dat<-U4_harmonise_data(IVs,Outs) %>%
#'  U4_add_eaf() %>%
#'   U4_check_samplesize()
#'
#' #### 查看样本量是否正确 ######################################
#'
#' # 如果ID过多，显示不全可以View(dat)查看"description_exposure"和"description_outcome"
#'
#' #### 暴露部分 ################################################
#'
#' # 假如Inflammatory bowel disease的样本量有错误，需要修改
#'
#' # 修改总样本量
#' dat$samplesize.exposure[ which(dat$exposure == "Inflammatory bowel disease" ) ]  <- 12000
#'
#' # 修改ncase
#' dat$ncase.exposure[ which(dat$exposure == "Inflammatory bowel disease" ) ]  <- 1200
#'
#' # 修改ncontrol
#' dat$ncontrol.exposure[ which(dat$exposure == "Inflammatory bowel disease" ) ]  <- 10800
#'
#' # 修改名字
#' dat$exposure[ which(dat$exposure == "Inflammatory bowel disease" ) ]  <- "IBD"
#'
#' #### 结局部分 ################################################
#'
#' # 假如HDL cholesterol实际上是二分类变量，需要修改
#'
#' # 增加ncase
#' dat$ncase.outcome[ which(dat$outcome == "HDL cholesterol" ) ]  <- 1200
#' # 增加ncontrol
#' dat$ncontrol.outcome[ which(dat$outcome == "HDL cholesterol" ) ]  <- 10800
#'
#'
#' # 对于结局名同样的Alzheimer's disease，如果想修改第一个Alzheimer's disease
#'
#' dat$outcome[ which(dat$id.outcome == "ieu-a-297" ) ]  <- "AD"
#'
#'
#' #### 最终确认 ################################################
#'
#' dat<-U4_check_samplesize(dat)
#'
#' ####################################################
#'
#' }
#'
U4_harmonise_data<- function(exposure_dat,
                             outcome_dat,
                             action = 2){

  message( "harmonise中......" )
  dat <- TwoSampleMR::harmonise_data(exposure_dat=exposure_dat,
                           outcome_dat=outcome_dat,
                           action = action)
  dat <- plyr::ddply(dat,
                     c("id.exposure", "id.outcome"),
                      function(x1) {
                        x <- subset(x1, mr_keep) %>%
                        dplyr::distinct(SNP,.keep_all = TRUE)
                        }   )
  dat <- plyr::ddply(dat,"id.exposure",
                      function(dat) {

                        if( length( unique(dat$samplesize.exposure) )>1 ){
                          message("暴露",dat$exposure[1],"有多个不同的样本量，只选择最大的样本量进行后续计算")
                        }

                        if( ( "ncase.exposure" %in% colnames(dat)) & ( "ncontrol.exposure" %in% colnames(dat) ) ){

                          dat$ncase.exposure<-  as.numeric(dat$ncase.exposure)
                          dat$ncontrol.exposure<-  as.numeric(dat$ncontrol.exposure)

                          samplesize.exposure <- as.numeric(  max(dat$samplesize.exposure) )

                          if( all(!is.na(dat$ncase.exposure))  & all( !is.na(dat$ncontrol.exposure)) ){

                            dat$type.exposure<- 'Binary'
                            dat$ncase.exposure<-dat$ncase.exposure[which(dat$samplesize.exposure == samplesize.exposure)[1] ]
                            dat$ncontrol.exposure<-dat$ncontrol.exposure[which(dat$samplesize.exposure == samplesize.exposure)[1] ]
                            dat$samplesize.exposure <- as.numeric(  max(dat$samplesize.exposure) )

                          }else{
                            dat$type.exposure<-"Continuous"
                            dat$samplesize.exposure <- as.numeric(  max(dat$samplesize.exposure) )
                          }

                        }else{
                          dat$samplesize.exposure <- as.numeric(  max(dat$samplesize.exposure) )
                          dat$type.exposure<-"Continuous"
                        }

                        return(dat)

                      } )

  dat <- plyr::ddply(dat,c("id.outcome"),
                      function(dat) {

                        if( length( unique(dat$samplesize.outcome) )>1 ){
                          message("结局",dat$outcome[1],"有多个不同的样本量，只选择最大的样本量进行后续计算")
                        }

                        if( ( "ncase.outcome" %in% colnames(dat)) & ( "ncontrol.outcome" %in% colnames(dat) ) ){

                          dat$ncase.outcome<-  as.numeric(dat$ncase.outcome)
                          dat$ncontrol.outcome<-  as.numeric(dat$ncontrol.outcome)

                          samplesize.outcome <- as.numeric(  max(dat$samplesize.outcome) )

                          if( all(!is.na(dat$ncase.outcome))  & all( !is.na(dat$ncontrol.outcome)) ){

                            dat$type.outcome<- 'Binary'
                            dat$ncase.outcome<-dat$ncase.outcome[which(dat$samplesize.outcome == samplesize.outcome)[1] ]
                            dat$ncontrol.outcome<-dat$ncontrol.outcome[which(dat$samplesize.outcome == samplesize.outcome)[1] ]
                            dat$ratio<-dat$ncase.outcome/dat$ncontrol.outcome
                            dat$samplesize.outcome <- as.numeric(  max(dat$samplesize.outcome) )

                          }else{
                            dat$type.outcome<-"Continuous"
                            dat$samplesize.outcome <- as.numeric(  max(dat$samplesize.outcome) )
                          }

                        }else{
                          dat$samplesize.outcome <- as.numeric(  max(dat$samplesize.outcome) )
                          dat$type.outcome<-"Continuous"
                        }
                        return(dat)
                      })

  if( any( is.na(dat$samplesize.exposure ) )  ){ warning("暴露存在NA的总样本量，请手动添加")   }

  if( any( is.na(dat$samplesize.outcome ) )  ){ warning("结局存在NA的总样本量，请手动添加")   }

  return( dat )

}





